package hongcaosp.app.android.user.dynamic;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.tencent.liteav.demo.play.SuperPlayerConst;

import hongcaosp.app.android.R;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.video.player.PlayVideoManager;
import hongcaosp.app.android.video.player.SimplePlayerView;
import hongcaosp.app.android.video.player.VideoProxy;
import xlj.lib.android.base.view.recyclepager.OnItemVisibleListener;

public class DynamicPlayerManager {
    private RecyclerView recyclerView;
    private Context context;
    private boolean isCreated;

    private int firstVisibleItem;
    private int lastVisibleItem;
    private boolean isToBottom;
    private LinearLayoutManager linearLayoutManager;

    public DynamicPlayerManager(final RecyclerView recyclerView, Context context) {
        this.recyclerView = recyclerView;
        this.context = context;
        final VisibleItemLinearLayoutManager mLayoutManager = new VisibleItemLinearLayoutManager(context, OrientationHelper.VERTICAL,false);
        recyclerView.setLayoutManager(mLayoutManager);
        linearLayoutManager = mLayoutManager;
        isCreated = true;
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE://停止滚动
                        /**在这里执行，视频的自动播放与停止*/
                        autoPlayVideo(recyclerView);
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING://拖动
//                        autoPlayVideo(recyclerView);
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING://惯性滑动
//                        pause();
                        break;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                isToBottom = dy > 0;
            }
        });
        linearLayoutManager = mLayoutManager;
        mLayoutManager.setOnViewPagerListener(new OnItemVisibleListener() {
            @Override
            public void onItemInit(int position, View view) {
            }

            @Override
            public void onItemRelease(boolean isNext, int position, View view) {
                if (recyclerView.getChildViewHolder(view) != null) {
                    VideoProxy videoProxy = (VideoProxy) recyclerView.getChildViewHolder(view);
                    videoProxy.stopVideo();
                    videoProxy.release();
                }
            }

            @Override
            public void onItemSelected(int position, boolean isBottom, View view) {
            }
        });
    }


    private long autoPlayTime;
    /**
     * 自动播放
     */
    private void autoPlayVideo(RecyclerView recyclerView) {
        if (autoPlayTime == 0) {
            autoPlayTime = System.currentTimeMillis();
        } else {
            long time = System.currentTimeMillis() - autoPlayTime;
            Log.i("playVideo","time = " + time);
        }
        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
        lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

        if (!isToBottom) {
            for (int i = lastVisibleItem; i >= firstVisibleItem; i--) {
                if (recyclerView == null || recyclerView.findViewHolderForAdapterPosition(i) == null) {
                    Log.i("playVideo", "no onPlay index = " + i);
                    break;
                }
                DynamicHolder videoProxy = (DynamicHolder) recyclerView.findViewHolderForAdapterPosition(i);
                if (videoProxy != null) {
                    if (!videoProxy.isVideoCover()) {
                        videoProxy.playVideo();
                        return;
                    }
                }
            }
        } else {
            for (int i = firstVisibleItem; i <= lastVisibleItem; i++) {
                if (recyclerView == null || recyclerView.findViewHolderForAdapterPosition(i) == null) {
                    Log.i("playVideo", "no onPlay index = " + i);
                    break;
                }
                DynamicHolder videoProxy = (DynamicHolder) recyclerView.findViewHolderForAdapterPosition(i);
                if (videoProxy != null) {
                    if (!videoProxy.isVideoCover()) {
                        videoProxy.playVideo();
                        return;
                    }
                }
            }
        }
    }

    public void release() {
        PlayVideoManager.getInstance().stop(context);
    }
}
